<?php

namespace app\api\controller\web;


use app\admin\model\Devicelock;
use app\admin\model\Org;
use app\admin\model\Role;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdevice;
use app\admin\model\Shoporder;
use app\admin\model\Shoproomset;
use app\admin\model\Shoptuan;
use app\admin\model\Shopwifi;
use app\admin\model\Tuanorder;
use app\admin\model\Userdoucode;
use app\common\controller\Api;
use OSS\OssClient;
use think\Config;

/**
 *
 */
class Dou extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function getList(){
        $page = $this->request->param('page');
        $startTime= $this->request->param('startTime');
        $endTime= $this->request->param('endTime');
        $mobile = $this->request->param('mobile');
        $shop_id = $this->request->param('shop_id');

        $where = array();
        $where2 = array();
        if( $shop_id != "-1"){
            $where['Userdoucode.shop_id'] = $shop_id;
            $where2['shop_id'] = $shop_id;
        }


        $user = $this->auth->getUser();

        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();

        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $limit = 10;
        $offset = ($page - 1) * $limit;
        $userDouCodeModel = new Userdoucode();
        $shopOrderModel = new Shoporder();
        $amount = $shopOrderModel->where($where2)->where('org_id',$org['id'])->where('status','not in',['paying','refund'])->where('dou_code',"<>",'')->sum('amount');


        $list = $userDouCodeModel->with(['user','coupon','shop','order'])->where($where)->where('Shop.org_id',$org['id'])->limit($limit,$offset)->order('create_time','desc')->select();
        $total = $userDouCodeModel->with(['shop'])->where($where)->where('Shop.org_id',$org['id'])->order('create_time','desc')->count('Userdoucode.id');

        $ret = array();
        foreach ($list as $key=>$val){

            $obj = array();
            $obj['dou_code'] = $val['dou_code'];
            $obj['mobile'] = $val['user']['mobile'];
            $obj['title'] = $val['coupon']['sku_name'];
            $obj['price'] = $val['coupon']['price'];
            $obj['shop_name'] = $val['shop']['name'];
            $obj['status'] = $val['status'];
            if($val['status'] == 'used'){
                $obj['status_txt'] = "已核销";
            }else{
                $obj['status_txt'] = "未核销";
            }
            $ret[] = $obj;

        }

        $result = array();
        $result['list'] = $ret;
        $result['total'] = $total;
        $result['totalAmount'] = $amount;

        $this->success('',$result);
    }


}
